package com.huluobo.a2111douyindemo.startLive;

import static com.tencent.live2.V2TXLiveCode.V2TXLIVE_ERROR_INVALID_LICENSE;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.Observer;

import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;

import com.blankj.utilcode.util.ToastUtils;
import com.huluobo.a2111douyindemo.R;
import com.huluobo.a2111douyindemo.databinding.ActivityStartLiveBinding;
import com.huluobo.mvvm_base.mvvm.base.view.BaseActivity;
import com.huluobo.mvvm_base.mvvm.base.view.BaseMVVMActivity;
import com.huluobo.mvvm_base.mvvm.bean.PushBeanResult;
import com.huluobo.mvvm_base.mvvm.bean.StopPushBeanResult;
import com.tencent.live2.V2TXLiveDef;
import com.tencent.live2.V2TXLivePlayer;
import com.tencent.live2.V2TXLivePusher;
import com.tencent.live2.impl.V2TXLivePlayerImpl;
import com.tencent.live2.impl.V2TXLivePusherImpl;
import com.tencent.rtmp.ui.TXCloudVideoView;

public class StartLiveActivity extends BaseMVVMActivity<ActivityStartLiveBinding, StartLiveViewModel> {

    private V2TXLivePusher mLivePusher;

    private V2TXLivePlayer mLivePlayer;

    String url = "rtmp://190253.push.tlivecloud.com/live/123?txSecret=facde014941d39ff9bb001f66335615a&txTime" +
            "=65EFB698";

    @Override
    protected void initData() {
        viewModel.startPushSuccess.observe(this, pushBeanResult -> {
            if (pushBeanResult.getCode() == 200) {
                mLivePlayer.startLivePlay(url);
            } else {
                ToastUtils.showLong(pushBeanResult.getMessage());
            }
        });

        viewModel.startPushFailed.observe(this, s -> {
            Log.i("TagA", "开播失败:" + s);
        });

        viewModel.stopPushSuccess.observe(this, stopPushBeanResult -> {
            Log.i("tagA", "下播成功:" + stopPushBeanResult.getMessage());
        });

        viewModel.stopPushFailed.observe(this, s -> {
            Log.i("TagA", "下播失败:" + s);
        });
    }

    @Override
    protected void initView() {
        // 指定对应的直播协议为 RTMP，该协议不支持连麦
        mLivePusher = new V2TXLivePusherImpl(this, V2TXLiveDef.V2TXLiveMode.TXLiveMode_RTMP);

        // 启动本地摄像头预览
        TXCloudVideoView mPusherView = (TXCloudVideoView) findViewById(R.id.pusher_tx_cloud_view);
        mLivePusher.setRenderView(mPusherView);
        mLivePusher.startCamera(true);
        mLivePusher.startMicrophone();

        dataBinding.startLiveStartPush.setOnClickListener(v -> {
            startPush();
        });

        dataBinding.startLiveStopPush.setOnClickListener(v -> {
            // 结束推流
            mLivePusher.stopPush();
            //下播
            stopPush();
        });

        //mPlayerView 即 step3 中添加的界面 view
        TXCloudVideoView mView = (TXCloudVideoView) findViewById(R.id.video_view);
        //创建 player 对象
        mLivePlayer = new V2TXLivePlayerImpl(this);
        //关键 player 对象与界面 view
        mLivePlayer.setRenderView(mView);

    }

    @Override
    protected Class<StartLiveViewModel> getViewModel() {
        return StartLiveViewModel.class;
    }

    private void stopPush() {
        viewModel.stopPush();
    }

    private void startPush() {
        // 根据推流协议传入对应的 URL 即可启动推流， RTMP 协议以 rtmp:// 开头，该协议不支持连麦

        int ret = mLivePusher.startPush(url);
        if (ret == V2TXLIVE_ERROR_INVALID_LICENSE) {
            Log.i("TagA", "startRTMPPush: license 校验失败");
        } else {
            if (!TextUtils.isEmpty(dataBinding.startLiveCaptionEt.getText().toString())) {
                viewModel.startPush(url, dataBinding.startLiveCaptionEt.getText().toString());
            } else {
                ToastUtils.showLong("直播描述不要为空");
            }
        }
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_start_live;
    }
}